﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class update_course : System.Web.UI.Page
{
    string Cno;
    string Cname;
    string category;
    string priority_no;
    string credit;
    string term;
    string teacher;
    string address;
    string time;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        Cno = Request.QueryString["Cno"];
        Cname = Request.QueryString["Cname"];
        category = Request.QueryString["category"];
        priority_no = Request.QueryString["priority_no"];
        credit = Request.QueryString["credit"];
        term = Request.QueryString["term"];
        teacher = Request.QueryString["teacher"];
        address = Request.QueryString["address"];
        time = Request.QueryString["time"];

        Label1.Text = Cno;
        Label2.Text = Cname;
        Label3.Text = category;
        Label4.Text = priority_no;
        Label5.Text = credit;
        Label6.Text = term;
        Label7.Text = teacher;
        Label8.Text = address;
        Label9.Text = time;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //string Cno = TextBox1.Text.Trim();
        Cname = TextBox2.Text.Trim();
        category = DropDownList1.Text.Trim();
        priority_no = TextBox3.Text.Trim();
        credit = TextBox4.Text.Trim();
        term = TextBox5.Text.Trim();
        teacher = TextBox6.Text.Trim();
        address = TextBox7.Text.Trim();
        time = TextBox8.Text.Trim();
        int temp = 0; //统计相同时间，相同地点上课的课程数，即冲突课程数


        string conn = "Data Source= . ;Initial Catalog=XSXK; User ID=sa; pwd=123";
        SqlConnection connection = new SqlConnection(conn);

        try
        {
            if ( Convert.ToInt16(credit)>18 || string.IsNullOrEmpty(Cname) || string.IsNullOrEmpty(category) || string.IsNullOrEmpty(priority_no) || string.IsNullOrEmpty(credit) || string.IsNullOrEmpty(term) || string.IsNullOrEmpty(teacher) || string.IsNullOrEmpty(address) || string.IsNullOrEmpty(time))
            {
                Response.Write("<h1>添加失败，填入的课程的信息有误！！</h1>");
            }
            else
            {
                connection.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = connection;

                string str_sql1 = " select count(*) from Course where Course.address='" + address + "' and Course.time='" + time + "' and term='" + term + "' ";
                comm.CommandText = str_sql1;
                temp = (int)comm.ExecuteScalar();

                if (temp == 0)
                {
                    string str_sql = " update Course set Cname='" + Cname + "', category='" + category + "' , priority_no='" + priority_no + "' , credit='" + credit + "' , term='" + term + "' , teacher='" + teacher + "' , address='" + address + "' , time='" + time + "' where Cno='"+ Cno +"' ";
                    comm.CommandText = str_sql;
                    comm.ExecuteNonQuery();

                    Response.Write("<h1>成功更新课程信息！</h1>");
                }
                else
                    Response.Write(" <h1>课程冲突，不能在同一时间同一地点上多节课！</h1> ");

            }
        }
        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }

        connection.Close();//关闭数据库连接
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

}